package com.pdf.controller;

import cn.hutool.core.lang.Assert;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.layout.font.FontProvider;
import lombok.SneakyThrows;
import org.apache.poi.util.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/")
public class DownloadController {



    @GetMapping("/download")
    public void downloadPdf(Long examId, HttpServletResponse response) {
//        Assert.notNull(examId, () -> new RuntimeException("当前试卷ID为空！"));
//
//        ExExamVo exExamVo = getExamInfo(examId);
//        AjaxResult ajaxResult = remoteUserService.getUser(exExamVo.getUserId());
//        UserDto user = FeignResponseUtil.analysisObj(ajaxResult, UserDto.class);
//        FuncUtil.handleIfTrue(Objects.nonNull(user), () -> exExamVo.setNickName(user.getNickName()));
//
//        Map<String, Object> dataModel = new HashMap<>();
//        dataModel.put("exam", exExamVo);
//
//        OutputStream os = response.getOutputStream();
//        try {
//            response.setContentType("application/pdf");
//            response.setCharacterEncoding("utf-8");
//            String fileName = URLEncoder.encode(SecurityUtils.getNickName() + ".pdf", "UTF-8");
//            response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
//            response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
//
//            FreemarkerUtil freemarkerUtil = FreemarkerUtil.getInstance(FreemarkerUtil.ClassForTemplateLoading, "/template");
//            String htmlContent = freemarkerUtil.generatorHtmlFromTemplate("exam.ftl",dataModel);
//
//            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//
//            ConverterProperties converterProperties = new ConverterProperties();
//            FontProvider provider = new FontProvider();
//
//            byte[] bytes = new byte[0];
//            PdfFont sysFont = PdfFontFactory.createFont("fonts/MiSans-Regular.ttf");
//            provider.addFont(sysFont.getFontProgram());
//            converterProperties.setFontProvider(provider);
//            HtmlConverter.convertToPdf(htmlContent, outputStream, converterProperties);
//
//            InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
//            bytes = IOUtils.toByteArray(inputStream);
//            os.write(bytes);
//            os.flush();
//        } catch (Exception e) {
//            log.error("导出 PDF 失败，请稍后重试", e);
//            throw new RuntimeException("导出 PDF 失败，请稍后重试");
//        } finally {
//            os.close();
//        }
    }


}
